//function object, a function with memory
//f and &f are not the same
//f(n) === (&f)->operator()(n)
#include <iostream>

using namespace std;

class Foo {
  int aa;
public:
  Foo(int a = 0):aa(a){cout<<"Foo()"<<aa<<endl;}
  void operator() (int& i); //function call
  void set(int a){aa  = a;}
  operator int() {cout<<"int()"<<endl;return aa;}
};

void Foo::operator()(int& i)
{
  int m = 1;
  for(int j = aa;j>0;--j) {
    m = m*i;
  }
  i = m;
}

int main()
{
  Foo f(3);
  int n;
  for(int i=0;i<10;i++) {
    n = i;(&f)->operator()(n);
    cout<<n<<"  ";
  }
  cout<<endl;
  //----------------------------------
  f.set(2);
  for(int i=0;i<10;i++) {
    n=i;
    f(n);
    cout<<n<<"  ";
  }
  cout<<endl;
  //----------------------------------
  f.set(0);
  for(int i=0;i<10;i++) {
    n = i;f(n);
    cout<<n<<"  ";
  }
  cout<<endl;
  cout<<(int)f<<endl;
  cout<<int(f)<<endl;
  return 0;
}
